package com.zlz.server.controller;


import com.zlz.server.annotation.OperateLog;
import com.zlz.server.aspect.OperateLogAspect;
import com.zlz.server.pojo.Joblevel;
import com.zlz.server.pojo.RespBean;
import com.zlz.server.service.IJoblevelService;
import com.zlz.server.enums.OprLevel;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author efire
 * @since 2021-01-24
 */
@RestController
@RequestMapping("/system/basic/joblevel")
public class JoblevelController {
    @Resource
    private IJoblevelService iJoblevelService;

    @ApiOperation(value = "获取所有职称信息")
    @GetMapping("/")
    public List<Joblevel> getAllJoblevels() {
        return iJoblevelService.list();
    }

    @ApiOperation(value = "添加职称信息")
    @PostMapping("/")
    @OperateLog(operationName = "添加职称信息", moduleName = "职称", level = OprLevel.LOW)
    public RespBean addJoblevel(@RequestBody Joblevel joblevel) {
        joblevel.setCreateDate(LocalDateTime.now());
        if (iJoblevelService.save(joblevel)) {
            return RespBean.success("添加成功", null);
        }
        return RespBean.error("添加失败");
    }

    @ApiOperation(value = "更新职称信息")
    @PutMapping("/")
    @OperateLog(operationName = "更新职称信息", moduleName = "职称", level = OprLevel.NORMAL)
    public RespBean updateJoblevel(@RequestBody Joblevel joblevel) {
        if (iJoblevelService.updateById(joblevel)) {
            return RespBean.success("更新成功", null);
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "删除职称信息")
    @DeleteMapping("/{id}")
    @OperateLog(operationName = "删除职称信息", moduleName = "职称", level = OprLevel.HIGH)
    public RespBean delJoblevel(@PathVariable("id") Long id) {
        if (iJoblevelService.removeById(id)) {
            return RespBean.success("删除成功", null);
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "批量删除职称信息")
    @DeleteMapping("/")
    @OperateLog(operationName = "批量删除职称信息", moduleName = "职称", level = OprLevel.HIGH)
    public RespBean deleteJoblevelsByIds(Long[] ids) {
        OperateLogAspect.setOperateDetail(new ArrayList<>(Arrays.asList(ids)).toString());
        if (iJoblevelService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("删除成功", null);
        }
        return RespBean.error("删除失败");
    }
}
